package org.winexpress

import grails.converters.JSON
import org.codehaus.groovy.grails.web.json.JSONObject
import org.codehaus.groovy.grails.web.json.JSONArray

class RatingCommentController {
    // shared methods
    def sendResponse(statusCode) {
        if (statusCode == 404) {
            JSONObject o = new JSONObject()
            o.put(statusCode, true)
            render o as JSON
        }
        
        response.status = statusCode
        render response
        
        // 200-OK (e.g. GET success)
        // 201-Created (e.g. POST success)
        // 403-Forbidden (e.g. LOGIN failure)
        // 400-Bad Request (e.g. PUT,POST,DELETE failure)
        // 404-Not Found (e.g. GET failure)
        // 500-Internal Server Error (other...)
    }
    
    

    // REST operations
    // GET
    def show = {
//        println 'RatingCommentController >> Show >> START'
        
        RatingCommentManagement rcman = new RatingCommentManagement()
        
        if (params.id && RatingComment.exists(params.id)) {    
            def rc = rcman.getRatingCommentByID(params.id)
            render rc as JSON
            sendResponse(200)
            
            println 'Show rating by ID >> ' + rc.id + " " + rc
        }
        else {
            def allRC = rcman.listRatingComments()
            if (allRC.length() > 0) {
                render allRC as JSON
                sendResponse(200)
            }
            else {
                sendResponse(404)
            }  
            
            println 'Listing all ratings comments >> ' + allRC
        }
        
//        println 'RatingCommentController >> Show >> END'
    }
    
    // PUT
    def update = {
//        println 'RatingCommentController >> Update >> START'
        
//        JSONObject o
//        def rc
//        def id
//        def wineType
//        def wineDesc
        
        RatingCommentManagement rcman = new RatingCommentManagement()
        
        
//        if (params.wineType && params.wineDesc) {
//            println 'RatingCommentController >> Update >> NO JSON'
//            rc = rcman.getRatingCommentByID(params.id)      
//            wineType = params.wineType
//            wineDesc = params.wineDesc
//            
////            rc.id = id
//            rc.wineTypeName = wineType
//            rc.description = wineDesc
//        }
//        else {
//            println 'RatingCommentController >> Update >> JSON'
//            o = request.JSON
//            
//            rc = rcman.getRatingCommentByID(o.get("id"))
//            if (o.has("wineTypeName")) { rc.wineTypeName = o.get("wineTypeName"); println rc.wineTypeName }
//            if (o.has("description")) { rc.description = o.get("description"); println rc.description }
//        }
//        
//        def result = rcman.updateRatingComment(rc)
//        if (result == true) {
//            println 'RatingCommentController >> Update >> ' + result
////            render response as JSON
//        }
//        else {
//            println 'RatingCommentController >> Update >> ' + result
//        }
           
        JSONObject o = request.JSON
        
        println o
            
        def rc = rcman.getRatingCommentByID(o.get("id"))
        if (o.has("rating")) { rc.rating = o.get("rating"); println rc.rating }
        if (o.has("comment")) { rc.comment = o.get("comment"); println rc.comment }
        
        def result = rcman.updateRatingComment(rc)
        if (result == true) {
            println 'RatingCommentController >> Update >> ' + result
            sendResponse(200)
        }
        else {
            println 'RatingCommentController >> Update >> ' + result
            sendResponse(400)
        }
        

//        println 'RatingCommentController >> Update >> END'

    }
    
    // DELETE
    def delete = {
//        println 'RatingCommentController >> delete >> START'
        
        RatingCommentManagement rcman = new RatingCommentManagement()
        
        if (params.id && RatingComment.exists(params.id)) {    
            def result = rcman.deleteRatingComment(id)
            
            if (result == true) {
                println 'RatingCommentController >> delete >> ' + result
                sendResponse(200)
            }
            else {
                println 'RatingCommentController >> delete >> ' + result
                sendResponse(400)
            }
        }
        
//        println 'RatingCommentController >> delete >> END'
    }
    
    // POST
    def save = {
//        println 'RatingCommentController >> Save >> START'
        
        JSONObject o = request.JSON
        println o
        
        RatingCommentManagement rcman = new RatingCommentManagement()
        
        def result = rcman.addRatingComment(o)
        if (result == true) {
            println 'RatingCommentController >> Save >> ' + result
            sendResponse(200)
        }
        else {
            println 'RatingCommentController >> Save >> ' + result
            sendResponse(400)
        }
        
//        println 'RatingCommentController >> Save >> END'
    }
}
